package test_activiti;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiTestCase;
import org.activiti.engine.test.Deployment;

public class 测试删除进程实例 extends ActivitiTestCase {

	//可以用逗号分隔开要部署的bpmn
	@Deployment(resources = {"bgApp.bpmn"})
	public void testDeleteProcInst() {
		    ProcessInstance pi = runtimeService.startProcessInstanceByKey("bgApp");
		    String piId = pi.getId();


		    System.out.println("已经删除的流程id:"+pi.getId());
		    Task task = taskService.createTaskQuery().taskAssignee("yw").singleResult();

		    System.out.println("formkey = "+task.getFormKey());

		    Map<String,Object> vmap = new HashMap<String,Object>();
		    vmap.put("sp_yw",1);
		    taskService.complete(task.getId(),vmap);

		    //先获取task js 的id,测试强制complete的情况
		    Task task_js = taskService.createTaskQuery().taskAssignee("js").singleResult();
		    String task_js_id=task_js.getId();

		    runtimeService.deleteProcessInstance(piId, "删除的理由是：嘻嘻嘻");

		    pi = runtimeService.createProcessInstanceQuery().processInstanceId(piId).singleResult();//the pi is null
		    System.out.println("pi == "+pi);

		    task = taskService.createTaskQuery().taskAssignee("js").singleResult(); //这里由于流程已经删除了，故task为null
			vmap = new HashMap<String,Object>();vmap.put("sp_js",1);
			if(task !=null)
				taskService.complete(task.getId(),vmap);

			//测试已经获取task id的情况，强制提交后由异常
//			taskService.complete(task_js_id,vmap); //这里抛出异常，说task找不到（由于其依赖的流程已经删除了）
			//org.activiti.engine.ActivitiObjectNotFoundException: Cannot find task with id 14



			//即使进程删除了，还是可以查到流程的历史的喔，包括流程变量那些
		    ActivitiUtils.printProcInstHistory(historicDataService,runtimeService,piId);
	}
}
